package com.xxun.xunaddnew;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.util.Log;


/**
 * Created by guohongcheng on 2017/9/14.
 */

public class AddFriendsService extends IntentService {

    private static final String name = "359076060015655";  // BtAddr
    private static final String machineSerialNo = "60015655"; //IMEI
    private static final String TAG = "AddFriendsService";
    private static final long MAXFILEZISE = 10 * 1000000;  // 10M
    public static OnUpdateShareView mOnUpdateShareView;
    String mEID = null;
    String mGID = null;
    String mImgOriginalPath = "/storage/emulated/0/DCIM/.thumbnails/1504180395200.jpg";
    String mVideoThumbnailPath = "/storage/emulated/0/DCIM/.thumbnails/1504180395200.jpg";
    String mVideoPath = "/storage/emulated/0/DCIM/Camera/VID_20160622_223428.mp4";
    private Context mContext;
    // @step 1
    // @step2
    private int fileType = -1;


    public AddFriendsService() {
        super(TAG);
    }

    public static void setOnUpdateShareView(OnUpdateShareView onUpdateShareView) {
        mOnUpdateShareView = onUpdateShareView;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String intentData = intent.getStringExtra(Constants.INTENT_BLUEADDR);
        String[] buleAddr = {intentData};

//        doLogin(buleAddr); // step2
//        setMsgListener(); //step3
//        initManager(mContext); //step4
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "[onDestroy] >> perform. ");
        super.onDestroy();
    }

    // @step2
//    private void doLogin(final String[] buleAddr) {
//        mMessageReceiveListener = new IMessageReceiveListener() {
//            @Override
//            public void onReceive(ResponseData responseData) {
//                //连接服务器成功后会下发消息码为ResponseData.RESPONSE_CODE_SOCKET_OK的通知，收到此//通知后才能进行登录动作
//                Log.d(TAG, "[MessageReceiveListener] >> onReceive. ");
//                if (responseData.getResponseCode() == ResponseData.RESPONSE_CODE_SOCKET_OK) {
//                    mXiaoXunNetworkManager.login(name,
//                            machineSerialNo, new IResponseDataCallBack<ResponseData>() {
//                                @Override
//                                public void onSuccess(ResponseData response) {
//                                    //TODO：登录成功后才能进行消息请求
//                                    Log.d(TAG, "[MessageReceiveListener] >> success. response is: " + response);
//
//                                    mEID = mXiaoXunNetworkManager.getWatchEid();
//                                    mGID = mXiaoXunNetworkManager.getWatchGid();
//
//                                    sendBlueAdr(buleAddr);
//                                    Log.d(TAG, "fileType: " + fileType);
//                                }
//
//                                @Override
//                                public void onError(int i, String s) {
//                                    //TODO：登录失败，需要查看具体的错误原因
//                                    Log.d(TAG, "[MessageReceiveListener] >> error. i/s : " + i + " / " + s);
//                                }
//                            });
//
//                } else if (responseData.getResponseCode() == ResponseData.RESPONSE_CODE_NOTICE) {
//                    //服务器下发的通知，根据不同的通知消息码进行相应的处理
//                    Log.d(TAG, "[MessageReceiveListener] >> server push info.");
//                }
//            }
//        };
//    }
//
//    // @step3
//    private void setMsgListener() {
//        Log.d(TAG, "[setListener] >> .");
//        mXiaoXunNetworkManager = XiaoXunNetworkManager.getInstance();
//        mXiaoXunNetworkManager.setMessageReceiveListener(mMessageReceiveListener);
//    }
//
//    // @step4
//    private void initManager(Context context) {
//        mXiaoXunNetworkManager.init(this, new IResponseDataCallBack<ResponseData>() {
//            @Override
//            public void onSuccess(ResponseData responseData) {
//                //TODO：服务初始化成功
//                Log.d(TAG, "[XiaoXunNetworkManager] init >> success. " + responseData);
//            }
//
//            @Override
//            public void onError(int i, String s) {
//                //TODO：服务初始化失败
//                Log.d(TAG, "[XiaoXunNetworkManager] init >> error. i/s : " + i + " / " + s);
//            }
//        });
//    }
//
//    private void clearAll() {
//        Log.d(TAG, "[clearAll] unset listener. ");
//        mXiaoXunNetworkManager.unsetMessageReceiveListener();
//        mXiaoXunNetworkManager.onDestroy();
//    }

    private void notifyUISuccess() {
        if (mOnUpdateShareView != null) {
            Message message = new Message();
            message.what = Constants.MSG_BT_SEARCH_SUCCESS;
            mOnUpdateShareView.onUpdateShareView(message);
            Log.d(TAG, "[uploadImage] >> send ui handler success.");
        } else {
            Log.d(TAG, "[uploadImage] >> mOnUpdateShareView is null, send ui handler fail.");
        }
//        clearAll();
    }

    private void notifyUIFail() {
        if (mOnUpdateShareView != null) {
            Message message = new Message();
            message.what = Constants.MSG_BT_SEARCH_FAIL;
            mOnUpdateShareView.onUpdateShareView(message);
            Log.d(TAG, "mOnUpdateShareView is null, update ui fail.");
        } else {
            Log.d(TAG, "mOnUpdateShareView is null, send ui handler fail.");
        }
//        clearAll();
    }

    private void sendBlueAdr(String[] blueAddr) {
//        blueAddr[] = {"E3:7B:7B:C4:E7:90"};
        Log.d(TAG, "[sendScanBluetoothAddress] >> blueAddr[0]: " + blueAddr[0]);
//        mXiaoXunNetworkManager.sendScanBluetoothAddress(blueAddr, new IResponseDataCallBack<ResponseData>() {
//            @Override
//            public void onSuccess(ResponseData responseData) {
//                Log.d(TAG, "[sendScanBluetoothAddress] >> success. response is: " + responseData);
//                notifyUISuccess();
//            }
//
//            @Override
//            public void onError(int i, String s) {
//                Log.d(TAG, "[sendScanBluetoothAddress] >> error. i/s : " + i + " / " + s);
//                notifyUIFail();
//            }
//        });
    }

    public interface OnUpdateShareView {
        void onUpdateShareView(Message message);
    }

}
